package server

import (
	"bufio"
	"fmt"

	"context"
	"os"
	"qq_service/save_file/protobuf/pb"
)

//服务结构体
type SaveFile struct {
	pb.UnimplementedSaveFileServiceServer
}

//HelloRequest请求
//HelloResponse返回结果
func SaveQQData(QQ []string, Savepath string) {
	savefile, _ := os.Create(Savepath)
	defer savefile.Close()
	saveall := bufio.NewWriter(savefile)
	for i := 0; i < len(QQ); i++ {
		fmt.Fprintln(saveall, QQ[i])
	}
	saveall.Flush()
}

func SaveQQEmailData(QQ []string, Savepath string) {
	savefile, _ := os.Create(Savepath)
	defer savefile.Close()
	saveall := bufio.NewWriter(savefile)
	for i := 0; i < len(QQ); i++ {
		fmt.Fprintln(saveall, QQ[i]+"@qq.com")
	}
	saveall.Flush()

}

func (h SaveFile) SaveFile(ctx context.Context, in *pb.QqListRequest) (*pb.QqListResponse, error) {
	resp := new(pb.QqListResponse)

	//resp.q_qpath
	path := "C:\\Users\\Tsinghua-yincheng\\Desktop\\day15\\tmp\\"
	QQpath := path + "qq-" + in.Email + ".txt"
	QQEmailpath := path + "qqemail-" + in.Email + ".txt"
	resp.QqPath = append(resp.QqPath, QQpath)
	resp.QqPath = append(resp.QqPath, QQEmailpath)
	SaveQQData(in.Qq, QQpath)           //保存文件
	SaveQQEmailData(in.Qq, QQEmailpath) //保存文件
	resp.Email = in.Email
	resp.Word = in.Word
	return resp, nil
}
